# -*- coding: utf-8 -*-
"""
Created on Sat Apr 30 17:06:35 2022

@author: fengmingshan
"""

import pandas as pd
import os
import PySimpleGUI as sg

import math

def add_cu_gateway(file,gw):
    with open(file,'a') as f:
        command1 = 'cr Transport=1\n'
        command2 = 'cr Transport=1,Router=S1_CU\n'
        command3 = 'cr Transport=1,Router=S1_CU,RouteTableIPv4Static=1\n'
        command4 = 'cr Transport=1,Router=S1_CU,RouteTableIPv4Static=1,Dst=1\n'
        command5 = '0.0.0.0/0\n'
        command6 = 'cr transport=1,Router=S1_CU,RouteTableIPv4Static=1,Dst=1,NextHop=1\n'
        command7 = '{}\n'.format(gw)
        command8 = 'false\n'
        command9 = 'NULL\n'
        command10 = '1\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines(command6)
        f.writelines(command7)
        f.writelines(command8)
        f.writelines(command9)
        f.writelines(command10)
        f.writelines('\n')
        f.writelines('\n')


def add_cu_ip(file,ip):
    with open(file,'a') as f:
        command1 = 'cr Transport=1,VlanPort=VLAN_S1_CU\n'
        command2 = 'EthernetPort=1\n'
        command3 = '50\n'
        command4 = '\n'
        command5 = 'cr Router=S1_CU,InterfaceIPv4=TN_B_S1_CU\n'
        command6 = 'Transport=1,VlanPort=VLAN_S1_CU\n'
        command7 = 'false\n'
        command8 = '\n'
        command9 = 'set Transport=1,VlanPort=VLAN_S1_CU$ userLabel VLAN Interface for S1_CU connection\n'
        command10 = 'cr Router=S1_CU,InterfaceIPv4=TN_B_S1_CU,AddressIPv4=1\n'
        command11 = '{}\n'.format(ip)
        command12 = '0\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines(command6)
        f.writelines(command7)
        f.writelines(command8)
        f.writelines(command9)
        f.writelines(command10)
        f.writelines(command11)
        f.writelines(command12)
        f.writelines('\n')
        f.writelines('\n')


def add_cu_sctp(file):
    with open(file,'a') as f:
        command1 = 'cr ENodeBFunction=1,AdditionalCoreNetwork=1\n'
        command2 = 'mcc=460,mnc=1,mncLength=2\n'
        command3 = '\n'
        command4 = 'cr Transport=1,SctpEndpoint=S1_CU\n'
        command5 = 'Transport=1,Router=S1_CU,InterfaceIPv4=TN_B_S1_CU,AddressIPv4=1\n'
        command6 = '36422\n'
        command7 = 'SctpProfile=1\n'
        command8 = '\n'
        command9 = 'set AdditionalCoreNetwork=1$ sctpS1Ref SctpEndpoint=S1_CU\n'
        command10 = 'set AdditionalCoreNetwork=1$ upS1IpAddressRef Router=S1_CU,InterfaceIPv4=TN_B_S1_CU,AddressIPv4=1\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines(command6)
        f.writelines(command7)
        f.writelines(command8)
        f.writelines(command9)
        f.writelines(command10)
        f.writelines('\n')
        f.writelines('\n')


def add_cu_mme(file):
    with open(file,'a') as f:
        command1 = 'crn ENodeBFunction=1,TermPointToMme=CUMME01\n'
        command2 = 'administrativeState 1\n'
        command3 = 'ipAddress1 10.100.4.160\n'
        command4 = 'ipAddress2 10.100.4.161\n'
        command5 = 'end\n\n'

        command6 = 'crn ENodeBFunction=1,TermPointToMme=CUMME02\n'
        command7 = 'administrativeState 1\n'
        command8 = 'ipAddress1 10.100.4.36\n'
        command9 = 'ipAddress2 10.100.4.37\n'
        command10 = 'end\n\n'

        command11 = 'crn ENodeBFunction=1,TermPointToMme=CUMME03\n'
        command12 = 'administrativeState 1\n'
        command13 = 'ipAddress1 10.100.4.98\n'
        command14 = 'ipAddress2 10.100.4.99\n'
        command15 = 'end\n\n'

        command16 = 'crn ENodeBFunction=1,TermPointToMme=CUMME04\n'
        command17 = 'administrativeState 1\n'
        command18 = 'ipAddress1 10.100.4.162\n'
        command19 = 'ipAddress2 10.100.4.163\n'
        command20 = 'end\n\n'

        command21 = 'set TermPointToMme=CUMME01$ additionalCnRef AdditionalCoreNetwork=1\n'
        command22 = 'set TermPointToMme=CUMME02$ additionalCnRef AdditionalCoreNetwork=1\n'
        command23 = 'set TermPointToMme=CUMME03$ additionalCnRef AdditionalCoreNetwork=1\n'
        command24 = 'set TermPointToMme=CUMME04$ additionalCnRef AdditionalCoreNetwork=1\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines(command6)
        f.writelines(command7)
        f.writelines(command8)
        f.writelines(command9)
        f.writelines(command10)
        f.writelines(command11)
        f.writelines(command12)
        f.writelines(command13)
        f.writelines(command14)
        f.writelines(command15)
        f.writelines(command16)
        f.writelines(command17)
        f.writelines(command18)
        f.writelines(command19)
        f.writelines(command20)
        f.writelines(command21)
        f.writelines(command22)
        f.writelines(command23)
        f.writelines(command24)
        f.writelines('\n')
        f.writelines('\n')


def active_feature(file):
    with open(file,'a') as f:
        command1 = 'set CXC4012095 featureState 1\n'
        command2 = 'set CXC4011823 featureState 1\n'
        command3 = 'set CXC4010960 featureState 1\n'
        command4 = 'set CXC4010956 featureState 1\n'
        command5 = 'set CXC4010616 featureState 1\n'

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines('\n')
        f.writelines('\n')


def add_two_plmn(file):
    with open(file,'a') as f:
        command1 = 'set1 EUtranCellFDD additionalPlmnList mcc=460,mnc=1,mncLength=2;mcc=1,mnc=1,mncLength=2;mcc=1,mnc=1,mncLength=2;mcc=1,mnc=1,mncLength=2;mcc=1,mnc=1,mncLength=2\n'
        f.writelines(command1)
        f.writelines('\n')
        f.writelines('\n')

def modity_tac(file, cell, tac):
    with open(file,'a') as f:
        command1 = 'Set EUtranCellFDD={cell} tac {tac}\n'.format(
            cell = cell,
            tac = tac,
        )
        f.writelines(command1)
        f.writelines('\n')


def add_external_enb(file,mnc,enb):
    with open(file,'a') as f:
        command1 = 'crn ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction={plmn}-{enb} \n'.format(
                plmn = '460'+str(int(mnc)),
                enb = enb,
                )
        command2 = 'eNBId {}\n'.format(enb)
        command3 = 'eNodeBPlmnId mcc=460,mnc={},mncLength=2\n'.format(mnc)

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines('end\n')
        f.writelines('\n')
        f.writelines('\n')


def add_external_enb_ip(file,mnc,enb,ip):
    with open(file,'a') as f:
        command1 = 'crn ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction={plmn}-{enb},TermPointToENB={plmn}-{enb}\n'.format(
                plmn = '460'+str(int(mnc)),
                enb = enb,
                )
        command2 = 'administrativeState 0\n'
        command3 = 'ipAddress {}\n'.format(ip)

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines('end\n')
        f.writelines('\n')
        f.writelines('\n')


def add_external_cell(file,mnc,enb,cellid,freq,pci,tac):
    with open(file,'a') as f:
        command1 = 'crn ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction={plmn}-{enb},ExternalEUtranCellFDD={plmn}-{enb}-{cellid}\n'.format(
                plmn = '460'+str(int(mnc)),
                enb = enb,
                cellid = cellid,
                )
        command2 = 'activePlmnList mcc=460,mnc={},mncLength=2\n'.format(mnc)
        command3 = 'eutranFrequencyRef ENodeBFunction=1,EUtraNetwork=1,EUtranFrequency={}\n'.format(freq)
        command4 = 'localCellId {}\n'.format(cellid)
        command5 = 'physicalLayerCellIdGroup {}\n'.format(math.floor(pci/3))
        command6 = 'physicalLayerSubCellId {}\n'.format(pci%3)
        command7 = 'tac {}\n'.format(tac)

        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines(command6)
        f.writelines(command7)
        f.writelines('end\n')
        f.writelines('\n')
        f.writelines('\n')


def add_relations(file,cellname,mnc,enb,cellid,freq):
    with open(file,'a') as f:
        command1 = 'crn ENodeBFunction=1,EUtranCellFDD={cellname},EUtranFreqRelation={freq},EUtranCellRelation={plmn}-{enb}-{cellid}\n'.format(
                cellname = cellname,
                plmn = '460'+str(int(mnc)),
                enb = enb,
                cellid = cellid,
                freq = freq,
                )
        command2 = 'neighborCellRef ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction={plmn}-{enb},ExternalEUtranCellFDD={plmn}-{enb}-{cellid}\n'.format(
                plmn = '460'+str(int(mnc)),
                enb = enb,
                cellid = cellid,
                )

        f.writelines(command1)
        f.writelines(command2)
        f.writelines('end\n')
        f.writelines('\n')
        f.writelines('\n')


def active_external_enb(file,mnc,enb):
    with open(file,'a') as f:
        command1 = 'lset ENodeBFunction=1,EUtraNetwork=1,ExternalENodeBFunction={plmn}-{enb},TermPointToENB={plmn}-{enb} administrativeState 1\n'.format(
                plmn = '460'+str(int(mnc)),
                enb = enb,
                )

        f.writelines(command1)
        f.writelines('\n')
        f.writelines('\n')

def run_share_parameters(file):
    with open(file,'a') as f:
        command1 = '''
            st EUtranCell UNLOCKED
            bl st_group
            set ENodeBFunction=1$ allowMocnCellLevelCommonTac True
            set ,UtranFreqRelation cellReselectionPriority 2
            set ,UtranFreqRelation csFallbackPrio 2
            set ,UtranFreqRelation mobilityActionCsfb 0
            set ,UeMeasControl=1$ ueMeasurementsActiveGERAN False
            set ,UtranFreqRelation allowedPlmnList mcc=460,mnc=01,mncLength=2
            set ,UtranFreqRelation anrMeasOn False
            set ,UtranFreqRelation connectedModeMobilityPrio -1
            $cellResePri[1850] = 4
            $cellResePri[1825] = 4
            $cellResePri[100] = 5
            $cellResePri[2452] = 3
            $cellResePri[2458] = 3

            mr MyCellFDD
            ma MyCellFDD ^EUtranCellFDD
            for $mo in MyCellFDD
              $cellName = ldn($mo)
              get $mo earfcndl$ > $arf
              set $cellName,EUtranFreqRelation=$arf$ allowedPlmnList mcc=460,mnc=01,mncLength=2;mcc=460,mnc=11,mncLength=2
              set $cellName,EUtranFreqRelation=$arf$ cellReselectionPriority $cellResePri[$arf]
              lset $cellName,EUtranFreqRelation=(1650|1506|350|3750|3714|3715|3740|300|400|325|425|250|3730)$ cellReselectionPriority $cellResePri[$arf]
            done

            lset EUtranCellFDD=.*,EUtranFreqRelation=(1650|1506|350|3750|3714|3715|3740|300|400|325|425|250|3730)$ mdtMeasOn false
            lset EUtranCellFDD=.*,EUtranFreqRelation=(1650|1506|350|3750|3714|3715|3740|300|400|325|425|250|3730)$ anrMeasOn false
            lset EUtranCellFDD=.*,EUtranFreqRelation=(1650|1506|350|3750|3714|3715|3740|300|400|325|425|250|3730)$ qRxLevMin -128

            lset EUtranCellFDD=.*,EUtranFreqRelation=(1650|1506|350|3750|3714|3715|3740|300|400|325|425|250|3730)$ allowedPlmnList mcc=460,mnc=01,mncLength=2
            deb st_group
        '''
        for line in [x for x in command1.split('\n') if x!= '\n']:
            f.writelines(line.strip())
            f.writelines('\n')
            f.writelines('\n')


def create_flex_counter(file):
    with open(file,'a') as f:
        command = '''
        crn ENodeBFunction=1,PmFlexCounterFilter=plmn0
        plmnFilterMin 0
        plmnFilterMax 0
        plmnFilterEnabled true
        end

        crn ENodeBFunction=1,PmFlexCounterFilter=plmn1
        plmnFilterMin 1
        plmnFilterMax 1
        plmnFilterEnabled true
        end

        for $idx = 1 to 9
        crn ENodeBFunction=1,PmFlexCounterFilter=plmn0qci$idx
        plmnFilterMin 0
        plmnFilterMax 0
        plmnFilterEnabled true
        qciFilterMin $idx
        qciFilterMax $idx
        qciFilterEnabled true
        end

        crn ENodeBFunction=1,PmFlexCounterFilter=plmn1qci$idx
        plmnFilterMin 1
        plmnFilterMax 1
        plmnFilterEnabled true
        qciFilterMin $idx
        qciFilterMax $idx
        qciFilterEnabled true
        end

        done

        hget PmFlexCounterFilter plmnFilter|qciFilter
        hget SharedNetworkSupport|CXC4010960$|CsfbToGeranUtran|CXC4010956$|WcdmaSessionContinuity|CXC4010616$ featurestate$|licensestate$
        '''
        for line in command.strip().split('\n'):
            f.writelines(line.strip()+'\n')


def make_cv(file):
    with open(file,'a') as f:
        command1 = '$date = `date +%Y%m%d`'
        command2 = 'cvms $date_LTE_open_CU_share'
        f.writelines(command1 +'\n')
        f.writelines(command2 +'\n')
        f.writelines('acc FieldReplaceableUnit=BPU-1  restartUnit'+'\n')
        f.writelines('\n')

def restart_node(file):
    with open(file,'a') as f:
        f.writelines('acc FieldReplaceableUnit=BPU-1  restartUnit'+'\n')
        f.writelines('\n')

def restart_node(file):
    with open(file,'a') as f:
        f.writelines('acc FieldReplaceableUnit=BPU-1  restartUnit'+'\n')
        f.writelines('\n')


def add_gran_freq_group(file):
    with open(file,'a') as f:
        f.writelines('cr ENodeBFunction=1,GeraNetwork=1\n')
        f.writelines('cr ENodeBFunction=1,GeraNetwork=1,GeranFreqGroup=900\n')
        f.writelines('1\n')
        f.writelines('cr ENodeBFunction=1,GeraNetwork=1,GeranFreqGroup=1800\n')
        f.writelines('2\n')
        f.writelines('\n')


def bind_freq_group_2_cell(file,cellnames):
    with open(file,'a') as f:
        for cellname in cellnames:
            f.writelines('cr ENodeBFunction=1,EUtranCellFDD={cellname},GeranFreqGroupRelation=1800'.format(cellname = cellname)+'\n')
            f.writelines('GeraNetwork=1,GeranFreqGroup=1800'+'\n')
            f.writelines('2' + '\n')
            f.writelines('cr ENodeBFunction=1,EUtranCellFDD={cellname},GeranFreqGroupRelation=900'.format(cellname = cellname)+'\n')
            f.writelines('GeraNetwork=1,GeranFreqGroup=900'+'\n')
            f.writelines('1' + '\n')
            f.writelines('\n')


def add_gran_freq(file,freq):
    if freq <= 124:
        band = 900
    else:
        band =1800
    with open(file,'a') as f:
        f.writelines('cr ENodeBFunction=1,GeraNetwork=1,GeranFrequency={band}_{freq}\n'.format(
                band = band,
                freq = freq,
                )
            )
        f.writelines('{}\n'.format(freq))
        f.writelines('0\n')
        f.writelines('set ENodeBFunction=1,GeraNetwork=1,GeranFrequency={band}_{freq} geranFreqGroupRef ENodeBFunction=1,GeraNetwork=1,GeranFreqGroup={band}\n'.format(
                band = band,
                freq = freq)
            )
        f.writelines('\n')


def add_utran_freq(file,band):
    with open(file,'a') as f:
        f.writelines('crn ENodeBFunction=1,UtraNetwork=1'+'\n')
        f.writelines('end'+'\n')
        f.writelines('\n')
        f.writelines('crn ENodeBFunction=1,UtraNetwork=1,GeranFreqGroup={band}'.format(band = band)+'\n')
        f.writelines('end'+'\n')
        f.writelines('\n')


def add_csfall_back2utran(file,freq,cellnames):
    with open(file,'a') as f:
        for cellname in cellnames:
            f.writelines('crn ENodeBFunction=1,EUtranCellFDD={cellname},UtranFreqRelation={freq}'.format(cellname = cellname,freq = freq)+'\n')
            f.writelines('cellReselectionPriority 0'+'\n')
            f.writelines('connectedModeMobilityPrio -1'+'\n')
            f.writelines('csFallbackPrio 2'+'\n')
            f.writelines('csFallbackPrioEC 0'+'\n')
            f.writelines('mobilityAction 2'+'\n')
            f.writelines('mobilityActionCsfb 1'+'\n')
            f.writelines('utranFrequencyRef ENodeBFunction=1,UtraNetwork=1,UtranFrequency={}'.format(freq)+'\n')
            f.writelines('end' + '\n')
            f.writelines('\n')


def add_gran_neighbor(file,band,freq):
    with open(file,'a') as f:
        f.writelines('cr ENodeBFunction=1,GeraNetwork=1,ExternalGeranCell={ext_name}'.format(ext_name = ext_name)+'\n')
        f.writelines('mcc=460,mnc=01,mncLength=2'+'\n')
        f.writelines('{lac}'.format(lac=lac) + '\n')
        f.writelines('{ci}'.format()+'\n')
        f.writelines('{priority}'.format()+'\n')
        f.writelines('{gms_nettype}'.format()+'\n')
        f.writelines('GeraNetwork=1,GeranFrequency={band}_{freq}'.format(band=band,freq=freq)+'\n')
        f.writelines('\n')

        f.writelines('cr ENodeBFunction=1,EUtranCellFDD={cell_name},GeranFreqGroupRelation=900,GeranCellRelation={relation_name}'.format(cell_name=cell_name,relation_name=relation_name)+'\n')
        f.writelines('GeraNetwork=1,ExternalGeranCell={ext_name}'.format(cell_name=cell_name,relation=relation)+'\n')
        f.writelines('\n')


sg.change_look_and_feel('DarkAmber')   # Add a style
# 定义GUI窗体布局
layout = layout = [[sg.Text('导入共享基站清单')],
                   [sg.Input('d:/'),
                    sg.FilesBrowse(file_types=(('任意格式', '*.*'),('Excel2007', '*.xlsx'), ('Excel2003', '*.xls'),), initial_folder='d:/')],
                   [sg.Text('导入邻区规划表')],
                   [sg.Input('d:/'),
                    sg.FilesBrowse(file_types=(('任意格式', '*.*'),('Excel2007', '*.xlsx'), ('Excel2003', '*.xls'),), initial_folder='d:/')],
                   [sg.Text('导入爱立信外部小区清单')],
                   [sg.Input('d:/'),
                    sg.FilesBrowse(file_types=(('任意格式', '*.*'),('Excel2007', '*.xlsx'), ('Excel2003', '*.xls'),), initial_folder='d:/')],
                    [sg.Button('生成脚本'),
                    sg.Button('退出')]]
# 新建窗体，引用之前定义好的布局
window = sg.Window('爱立信共享脚本制作      (软件开发:肖晶晶)', layout,default_element_size=(60,1))

while True:
    event, values = window.read()
    share_file = values[0]
    neighbor_file = values[1]
    external_file = values[2]
    out_path = r'D:/_爱立信共享脚本输出/'

    if not os.path.exists(out_path):
        os.mkdir(out_path)
    os.chdir(out_path)

    if event in (None, '退出'):  # if user closes window or clicks cancel
        break
    elif event in ('生成脚本'):
        df_neighbor = pd.read_excel(neighbor_file)
        df_externalENb = pd.read_excel(external_file)
        df_share = pd.read_excel(share_file)
        df_share_cell = pd.read_excel(share_file,sheet_name = '小区清单')
        df_cs_fallback = pd.read_excel(share_file,sheet_name = '小区清单')
        df_cs_fallback = df_cs_fallback[['ENODEBID','cellName','回落策略','频段','频点']]


        externalENb_set = set(df_externalENb['eNBid'].unique()) # 外部基站的集合

        for i in range(len(df_share)):  # 迭代需要开放共享的基站清单针对每个enb生成添加邻区指令
            enb = df_share.loc[i,'ENODEBID']

            add_cu_gateway(
                './{}.mos'.format(enb),
                df_share.loc[i,'联通网关'],
            )
            add_cu_ip(
                './{}.mos'.format(enb),
                df_share.loc[i,'联通IP'],
            )
            add_cu_sctp( './{}.mos'.format(enb))
            add_cu_mme( './{}.mos'.format(enb))
            active_feature( './{}.mos'.format(enb))
            add_two_plmn( './{}.mos'.format(enb))

            df_cell = df_share_cell[df_share_cell['ENODEBID']==enb]
            df_cell.reset_index(drop = True, inplace =True)
            for i in range(len(df_cell)):
                modity_tac(
                    './{}.mos'.format(enb),
                    df_cell.loc[i,'cellName'],
                    df_cell.loc[i,'TAC'],
                )

            create_flex_counter(
                './{}.mos'.format(enb),
            )

            run_share_parameters(
                './{}.mos'.format(enb),
            )

            # 制作添加邻区脚本
            # 筛选目标小区为联通的邻区
            df_tmp = df_neighbor[df_neighbor['电信基站ID']== enb]
            df_tmp.reset_index(drop = True,inplace = True)

            if not df_tmp.empty: # 判断是否有要添加的邻区
                df_enb = df_tmp[['目标小区基站ID','业务IP','mnc']] # 取enb和S1IP两列数据，因为添加外部基站只用到这两列
                df_enb = df_enb[~df_enb['目标小区基站ID'].isin(externalENb_set)] # 筛选网管没有配置的外部基站
                df_enb.drop_duplicates('目标小区基站ID',inplace =True) # 去重
                df_enb.reset_index(drop = True,inplace = True) # 重排序号方便后续迭代
                if not df_enb.empty: # 添加外部基站和基站S1IP
                    for i in range(len(df_enb)):
                        add_external_enb(
                            './{}_neighbor.mos'.format(enb),
                            df_enb.loc[i,'mnc'],
                            df_enb.loc[i,'目标小区基站ID'],
                        )
                        add_external_enb_ip(
                            './{}_neighbor.mos'.format(enb),
                            df_enb.loc[i,'mnc'],
                            df_enb.loc[i,'目标小区基站ID'],
                            df_enb.loc[i,'业务IP'],
                        )
                for i in range(len(df_tmp)):
                    add_external_cell(
                        './{}_neighbor.mos'.format(enb),
                        df_tmp.loc[i,'mnc'],
                        df_tmp.loc[i,'目标小区基站ID'],
                        df_tmp.loc[i,'目标小区ID'],
                        df_tmp.loc[i,'目标小区频点'],
                        df_tmp.loc[i,'目标小区PCI'],
                        df_tmp.loc[i,'目标小区TAC'],
                    )

                    add_relations(
                        './{}_neighbor.mos'.format(enb),
                        df_tmp.loc[i,'网管小区名'],
                        df_tmp.loc[i,'mnc'],
                        df_tmp.loc[i,'目标小区基站ID'],
                        df_tmp.loc[i,'目标小区ID'],
                        df_tmp.loc[i,'目标小区频点'],
                    )
                    active_external_enb(
                        './{}_neighbor.mos'.format(enb),
                        df_tmp.loc[i,'mnc'],
                        df_tmp.loc[i,'目标小区基站ID'],
                    )

        enbs = list(set(df_cs_fallback['ENODEBID'].unique()))
        for enb in enbs:
            df_tmp1 = df_cs_fallback[df_cs_fallback['ENODEBID']==enb]
            if df_tmp1['回落策略'].unique()[0] == '2G':
                freq_list = df_tmp1['频点'].unique()[0].split(',')
                freq_list = [int(x.strip()) for x in freq_list if x!= '']
                add_gran_freq_group(
                        './{}.mos'.format(enb),
                        )
                bind_freq_group_2_cell(
                        './{}.mos'.format(enb),
                        df_tmp1['cellName']
                        )
                for freq in freq_list:
                    add_gran_freq(
                            './{}.mos'.format(enb),
                            freq
                            )
            else:
                freq = df_tmp1['频点'].unique()[0]
                add_utran_freq(
                        './{}.mos'.format(enb),
                        freq
                        )
                add_csfall_back2utran(
                    './{}.mos'.format(enb),
                    freq,
                    df_tmp1['cellName']
                    )

        sg.Print('脚本已生成,并输出到 {}'.format(out_path))
        os.startfile(out_path)

